package cn.cumulomq.service;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import cn.cumulomq.dao.TeleMessageDao;
import cn.cumulomq.db.MessageDB;
import cn.cumulomq.entity.Page;
import cn.cumulomq.entity.TeleMessage;

/**
 * 收发报文查询与存储
 * 
 * @author maxuedong
 *
 */
public class TeleMessageService {
	private Logger logger = Logger.getLogger(TeleMessageService.class);

	private String originatorAddress;
	/**
	 * 构造方法，需要设置发件地址
	 * @param originatorAddress
	 */
	public TeleMessageService(String originatorAddress){
		this.originatorAddress = originatorAddress;
	}
	public String getOriginatorAddress() {
		return originatorAddress;
	}
	public void setOriginatorAddress(String originatorAddress) {
		this.originatorAddress = originatorAddress;
	}
	/**
	 * 根据ID获取teleMessage
	 * @param id
	 * @return cn.cumulomq.entity.TeleMessage
	 */
	public TeleMessage get(String id) {
		TeleMessage teleMessage = null;
		try {
			SqlSession session = MessageDB.getSessionFactory(this.originatorAddress).openSession();
			TeleMessageDao teleMessageDao = session.getMapper(TeleMessageDao.class);
			teleMessage = teleMessageDao.get(id);
			session.close();
		} catch (Exception e) {
			logger.error(e);
		}
		return teleMessage;
	}
	/**
	 * 添加或修改TeleMessage，ID为空添加，ID不为空修改
	 * @param teleMessage
	 * @return boolean
	 */
	public boolean save(TeleMessage teleMessage){
		try{
			SqlSession session = MessageDB.getSessionFactory(originatorAddress).openSession();
			TeleMessageDao teleMessageDao = session.getMapper(TeleMessageDao.class);
			if(StringUtils.isBlank(teleMessage.getId())){
				teleMessage.setId(UUID.randomUUID().toString());
				teleMessageDao.insert(teleMessage);
			}else{
				teleMessageDao.update(teleMessage);
			}
			session.commit();
			session.close();
		}catch(Exception e){
			logger.error(e);
			return false;
		}

		return true;
	}
	/**
	 * 删除报文
	 * @param teleMessage
	 * @return
	 */
	public boolean delete(TeleMessage teleMessage){
		try{
			SqlSession session = MessageDB.getSessionFactory(originatorAddress).openSession();
			TeleMessageDao teleMessageDao = session.getMapper(TeleMessageDao.class);
			teleMessageDao.delete(teleMessage.getId());
			session.commit();
			session.close();
		}catch(Exception e){
			logger.error(e);
			return false;
		}

		return true;
	}
	/**
	 * 查询所有报文
	 * @param teleMessage
	 * @param pageSize
	 * @param pageNO
	 * @return
	 */
	public Page<TeleMessage> query(TeleMessage teleMessage,int pageSize,int pageNO){
		Page<TeleMessage> page = new Page<TeleMessage>();
		page.setPageNO(pageNO);
		page.setPageSize(pageSize);
		try{
			SqlSession session = MessageDB.getSessionFactory(originatorAddress).openSession();
			TeleMessageDao teleMessageDao = session.getMapper(TeleMessageDao.class);
			List<TeleMessage> list = teleMessageDao.query(teleMessage,pageSize,pageNO);
			page.setList(list);
			page.setRecordCount(teleMessageDao.count(teleMessage));
			session.close();
		}catch(Exception e){
			logger.error(e);
		}
		return page;
	}
}
